<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use App\Admin\Renderables\TeamRenderables;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use Dcat\Admin\Admin;
use App\Handlers\Team;


class AdminUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdminUser(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id->sortable();
            $grid->username;
            $grid->password;
            $grid->name;
            $grid->avatar;
            $grid->remember_token;
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdminUser(), function (Show $show) {
            $show->id;
            $show->username;
            $show->password;
            $show->name;
            $show->avatar;
            $show->remember_token;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminUser(), function (Form $form) {
            $form->display('id');
            $form->text('username');
            $form->text('password');
            $form->text('name');
            $form->text('avatar');
            $form->text('remember_token');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


    public function userteam(Content $content)
    {
        //dd(explode('/', request()->path()));

        // 添加面包屑导航
        $content->breadcrumb(
            ['text' => trans('handlers.admin_user_tdglxxxx'), 'url' => '/user_team'],
            ['text' => '']
        );

        return $content
            ->title(trans('handlers.admin_user_tdglxxxx'))
            ->description('')
            //->description($this->description()['index'] ?? trans('admin.list'))
            ->body(new TeamRenderables());
    }


    protected function team_data(Request $request)
    {
        // 搜索功能
        $account = $request->account ?? '100001';
        $type    = $request->type;      // type 为 1 把自身数组加上去
        $times   = $request->times;     // 时间查询

        // 搜索功能 只搜索直推的人
        $queryUser = AdminUser::where('username', $account)->first();// 查询用户

        if (!$queryUser) {
            return self::returnJson('', -1, trans('handlers.admin_user_wcyhxxxx'));
        }

//        if (!Team::isVerifyUserUp($queryUser)){
//            return self::returnJson('', -1, trans('handlers.admin_user_csywxxxx'));
//        }

        // 查询 子孙数组
        $new = Team::treeToData2($account, $type, $times);
        if ($new) {
            return array("code" => 0, "msg" => trans('handlers.admin_user_cxcgxxxx'), "data" => $new);
        } else {
            return array("code" => -1, "msg" => trans('handlers.admin_user_cxsbxxxx'), "data" => '');
        }
    }

    /***
     * 查询用户投入流水
     * @param  Request  $request  接收的查询参数
     * @return \Illuminate\Http\JsonResponse
     */
    protected function my_money(Request $request)
    {
        // 搜索功能 只搜索直推的人
        $queryUser = AdminUser::with(['deposite'])->where('id', $request->admin_user)->first();// 查询用户

        if (!Team::isVerifyUserUp($queryUser)) {
            return self::returnJson('', 2, trans('handlers.admin_user_csywxxxx'));
        }

        $deposite = $queryUser->deposite->whereIn('type', [1, 2]);
        $str      = Team::getUserOrder($deposite);

        if (!empty($str)) {
            return self::returnJson($str, 1);

        } else {
            return self::returnJson('');
        }
    }


    /***
     * @param          $data     要返回的数组
     * @param  int     $code     状态码
     * @param  string  $message  返回信息
     * @return \Illuminate\Http\JsonResponse
     */
    protected function returnJson($data, $code = 0, $message = '')
    {
        return response()->json([
            'code'    => $code,
            'data'    => $data,
            'message' => $message
        ]);
    }

}
